package com.ocean.redflagtest.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ocean.redflagtest.mvvm.models.entity.OriginalModel;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by ocean on 2018/8/1
 * Author :  ocean
 * Email  :  348686686@qq.com
 */
public class OceanUtils {

    public static class Holder {
        public static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient();
        public static final Gson GSON = new Gson();
    }

    public static void logE(Object o) {
        Log.e("ocean", "日志打印： " + o.toString());
    }

    /**
     * px转换成dp
     */
    public static int px2dp(Context context, float pxValue) {
        OceanUtils.logE("打印传参点" + pxValue);
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static void HttpGet(String url, final Handler handler) {
        OkHttpClient okHttpClient = Holder.OK_HTTP_CLIENT;
        Request request = new Request.Builder()
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Message message = new Message();
                message.what = 200;
                message.obj = result;
                handler.sendMessage(message);
            }
        });
    }

    public static List<OriginalModel> getOriginalList(String result) {
        return Holder.GSON.fromJson(result, new TypeToken<List<OriginalModel>>() {
        }.getType());
    }


}
